const app = getApp();
import { request } from '../../utils/request';
import config from '../../utils/config'
Page({
    data: {
        custom: wx.getMenuButtonBoundingClientRect(),
        picUrl: config.picUrl,
        user: {},
        priceList: [],
        publisherCount: 0,
        priceIndex: 0,
        agree:false
    },
    onLoad(options) {
        var timer = setInterval(() => {
            var user = wx.getStorageSync('userInfo')
            if (user) {
                this.setData({ user })
                clearInterval(timer)
            }
        }, 500)
        this.getPriceList()
        this.getPublisherCount()
    },
    getPriceList() {
        request({
            url: '/miniprogram/user/getPriceList',
            data:{type:2}
        }).then(res => {
            if (res.code == 200) {
                this.setData({
                    priceList: res.data,
                })
            }
        })
    },
    getPublisherCount() {
        request({
            url: '/miniprogram/system/getPublisherCount'
        }).then(res => {
            this.setData({ publisherCount: res.data.count })
        })
    },
    changePrice(e) {
        var index = e.currentTarget.dataset.index
        this.setData({ priceIndex: index })
    },
    buyMember() {
        if(!this.data.agree){
            config.toast('请先阅读并同意《会员协议》。',201,()=>{})
            return false
        }
        var user = this.data.user,
            price = this.data.priceList[this.data.priceIndex],
            _this = this
        request({
            url: '/miniprogram/lklpay/buyMenber',
            data: { user_id: user.id, openid: user.openid, member_price: price },
            type: 'post'
        }).then(res => {
            if(res.code != 200){
                config.toast(res.msg,201,()=>{})
                return false
            }
            wx.requestPayment({
                timeStamp: res.data.acc_resp_fields.time_stamp,
                nonceStr: res.data.acc_resp_fields.nonce_str,
                package: res.data.acc_resp_fields.package,
                signType: res.data.acc_resp_fields.sign_type,
                paySign: res.data.acc_resp_fields.pay_sign,
                success(rel) {
                    wx.showLoading()
                    setTimeout(()=>{
                        request({ url: '/miniprogram/user/checkMemberOrder', data: { out_trade_no: res.data.out_trade_no, user_id: user.id } }).then(res2 => {
                            wx.hideLoading()
                            wx.showToast({ title: res2.msg, icon: 'none'})
                            if(res2.code == 202){
                                setTimeout(()=>{
                                    wx.navigateTo({url:"/pages/shanghurenzheng2/shanghurenzheng2"})
                                },1500)
                            }else{
                                setTimeout(()=>{
                                    wx.navigateBack({delta:1})
                                },1500)
                            }
                        })
                    },2000)
                },
                fail(err) {
                    wx.showToast({ title: '取消支付', icon: 'none' })
                }
            })
        })
    },
    checkbox(e){
        this.setData({agree:!this.data.agree})
    }
})